package com.longjianghuasheng.logistics.business.main

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import androidx.activity.OnBackPressedCallback
import androidx.navigation.findNavController
import com.blankj.utilcode.util.LanguageUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseActivity
import com.longjianghuasheng.logistics.databinding.ActivityMainBinding
import com.longjianghuasheng.logistics.ext.toastNormal
import com.xuexiang.xui.utils.StatusBarUtils
import com.xuexiang.xui.widget.toast.XToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.network.manager.NetState

/**
 * 项目主页Activity
 */
class MainActivity : BaseActivity<BaseViewModel, ActivityMainBinding>() {

    var exitTime = 0L
    override fun layoutId() = R.layout.activity_main

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initStatusBar()
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LanguageUtils.attachBaseContext(newBase))
    }

    override fun initView(savedInstanceState: Bundle?) {
        //进入首页检查更新
//        Beta.checkUpgrade(false, true)

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val nav = findNavController(R.id.host_fragment)
                if (nav.currentDestination != null && nav.currentDestination!!.id != R.id.main2Fragment) {
                    //如果当前界面不是主页，那么直接调用返回即可
                    nav.navigateUp()
                } else {
                    //是主页
                    if (System.currentTimeMillis() - exitTime > 2000) {
                        toastNormal(R.string.exit_program_reminder)
                        exitTime = System.currentTimeMillis()
                    } else {
                        finish()
                    }
                }
            }
        })
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)


        //XUI  toast
        XToast.Config.get() //位置设置为居中
            .setGravity(Gravity.CENTER)

    }

    private fun initStatusBar() {
        StatusBarUtils.initStatusBarStyle(this, false)
    }

    override fun createObserver() {

    }

    /**
     * 示例，在Activity/Fragment中如果想监听网络变化，可重写onNetworkStateChanged该方法
     */
    override fun onNetworkStateChanged(netState: NetState) {
        super.onNetworkStateChanged(netState)
//        if (netState.isSuccess) {
//            Toast.makeText(
//                applicationContext,
//                "我tm终于有网了！",
//                Toast.LENGTH_SHORT
//            ).show()
//        } else {
//            Toast.makeText(applicationContext, "我tm怎么断网了", Toast.LENGTH_SHORT)
//                .show()
//        }
    }


    companion object {
        private const val TAG = "MainActivity"
    }
}
